/**
 * @author LKQ
 * @date 2022/2/18 14:25
 * @description
 */
public class FormalSolution {
    public static void main(String[] args) {
        TreeNode t1 = new TreeNode(3), t2 = new TreeNode(1);
        TreeNode t3 = new TreeNode(2, null, t1), t4 = new TreeNode(3, null, t2);
        TreeNode t5 = new TreeNode(3, t3, t4);
        FormalSolution formalSolution = new FormalSolution();
        formalSolution.rob(t5);

    }
    public int rob(TreeNode root) {
        // 用数组保存两个状态，偷根节点的最大金额和不偷根节点的最大金额
        int[] rootStatus = dfs(root);
        return Math.max(rootStatus[0], rootStatus[1]);
    }

    public int[] dfs(TreeNode node) {
        if (node == null) {
            return new int[]{0, 0};
        }
        int[] l = dfs(node.left);
        int[] r = dfs(node.right);
        int selected = node.val + l[1] + r[1];
        int notSelected = Math.max(l[0], l[1]) + Math.max(r[0], r[1]);
        return new int[]{selected, notSelected};
    }

}
